<?php

if (!defined('TOBBIVM'))
	header('location:/template/notrepassing.php');
/**
 * This file is part of TobbiVMShop.
 *
 * TobbiVMShop is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TobbiVMShop is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TobbiVMShop.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * create article-detail browse page
 * get: XXX.tpl.php
 * used from core.php
 *
 * @version    $Id$
 * @package    TobbiVM-Shop
 * @copyright  Copyright (C) 2012
 * @author     Norbert Gebert
 * @license    GPL3
 */
function contentPage($refPage, $page, $title, $name, $refValidate)
{
	$pagefile = 'module/function/price.calculate.php';
	if (is_file($pagefile))
		include_once($pagefile);
	// include show-options-class
	$pagefile = 'module/class/option.show.class.php';
	if (is_file($pagefile))
		include_once($pagefile);
	// include options-calculate-class
	$pagefile = 'module/class/option.calculate.class.php';
	if (is_file($pagefile))
		include_once($pagefile);

	// Read article-data from the selectet article
	db::getInstance()->where('product_id', $title);
	db::getInstance()->where('product_active', '1');
	db::getInstance()->where('product_stocks_quantity', '1', '>=');
	$result = db::getInstance()->getOne('product');

	// Create metakeys and -description
	if (!empty($result['product_metakey']))
	{
		$meta = $result['product_metakey'] . ', ' . META_KEYWORDS;
		$metax = explode(',', $meta);
		$meta = '';

		for ($i = 0; $i < META_KEYWORD_NUMBER; $i++)
		{
			$meta .= $metax[$i];
			if ($i < META_KEYWORD_NUMBER - 1)
				$meta .=',';
		}
		$refPage->assign('Metakeywords', $meta);
	}

	if (!empty($result['product_metadescription']))
	{
		$meta = $result['product_metadescription'] . ', ' . META_DESCRIPTION;
		$meta = substr($meta, 0, META_DESCRIPTION_LENGH - 1) . '&hellip;';
		$refPage->assign('Metadescription', $meta);
	}

	$pagefile = 'module/function/articlecounter.php';
	if (is_file($pagefile))
		include_once($pagefile);

	$refPage->assign('sku', $result['product_sku']);

	$pageText = $result['product_description'];
	$pageText = xplugin::getPluginCode($pageText);
	$texte = explode('{BOTTOM}', $pageText);

	$refPage->assign('textTop', $texte[0]);

	if (count($texte) == 2)
	{
		$refPage->assign('textBottom', $texte[1]);
	}
	else
	{
		$refPage->assign('textBottom', '');
	}

	$refPage->assign('url', $result['product_url']);
	$refPage->assign('inStock', $result['product_stocks_quantity']);
	$refPage->assign('sales', $result['product_sales']);
	$refPage->assign('currency', CURRENCY);
	$refPage->assign('attribute', $result['product_name']);
	$refPage->assign('name', $result['product_name']);
	$refPage->assign('createdate', $result['product_create']);
	$refPage->assign('changedate', $result['product_last_change']);

	// Buy-row
	$refPage->assign('formStart',
		'<form action="' . URL . '?page=article&title=' .
		$result['product_id'] . '&name=' . $result['product_name'] .
		'&cart=add"' . 'method="post">');
	$refPage->assign('formEnd', '</form>');
	// Create 'buy-button'...
	$refPage->assign('number',
		'<input oninput="newPrice()" name="number" id="number" value="1" size="4" type="text">');
	$refPage->assign('buyButton',
		'<input type="hidden" name="idx" value="' . $result['product_id'] . '">' .
		'<input class="buyButton" value="' . _('buy now') . '" title="' . _('buy now') .
		'" type="submit">'
	);

	// Create image path
	$refPage->assign('smallimage',
		'/images/' . ARTICLE_IMAGE_PATH . '/' . $result['product_category'] . '/' .
		ARTICLE_IMAGE_THUMB_PATH . '/' . $result['product_small_image']);
	$refPage->assign('image',
		'/images/' . ARTICLE_IMAGE_PATH . '/' . $result['product_category'] . '/' .
		$result['product_small_image']);

	// Options html-Block
	$options = new optionsShow($result['product_attribute_id']);
	$refPage->assign('option', $options->getOptionHtml());

	// Calculate price and tax
	$price = array();
	$tax = array();
	$price = getSingleArticlePriceWithTax($result['product_price_id'],
		$result['product_tax_id']);
	$html = '';

	if ($price['priceTyp'] === 3)
	{ // Create season price list
		$rows = explode("\r\n", $price['seasonPriceList']);
		$html = '<table cellspacing="0" cellpaddin="0" class="seasonPriceList")';
		$html .='<thead><tr><th class="colh1">' . _('amount') . '</th><th class="colh2">' .
			_('to amount') . '</th><th class="colh3">' . $price['productUnit'] . '</th></tr></thead>';

		foreach ($rows as $row)
		{
			$values = explode(':', $row);
			$value = explode('-', $values[0]);
			$html .= '<tr><td class="col1">' . $value[0] . '</td>' .
				'<td class="col2">' . $value[1] . '</td>' .
				'<td class="col3">' . $values[1] . CURRENCY . '</td></tr>';
		}
		$html .= '</table>';
	}

	$refPage->assign('seasonPriceList', $html);
	$pricex = floatval(str_replace(',', '.', str_replace('.', '', $price['productPrice'])));
	$tax = getArticleTax($result['product_tax_id'], $pricex);

	$refPage->assign('price', '<span id="sprice">' . $price['productPrice'] . '</span>');
	$refPage->assign('productUnit', $price['productUnit']);
	$refPage->assign('productJs', $price['productJs']);

	if ($pricex > 0)
	{
		$refPage->assign('tax', '<span id="stax">' . $tax['productTax'] . '</span>');
		$refPage->assign('taxValue', $tax['productTaxValue']);
	}
	else
	{
		$refPage->assign('tax', '0,00');
		$refPage->assign('taxValue', '0');
	}

	$refPage->addtpl('content',
		'article.browse/' . $result['product_browsepage'] .
		'.tpl.php');
}

?>